package com.dongdongshop.controller;

import cn.hutool.json.JSONUtil;
import com.dongdongshop.data.Result;
import com.dongdongshop.enums.ResultEnums;
import com.dongdongshop.pojo.*;
import com.dongdongshop.service.TbGoodsService;
import com.dongdongshop.vo.GootsVO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("TbGoods")
public class TbGoodsController {

    //注入controller
    @DubboReference
    private TbGoodsService tbGoodsService;
    //rocketMQTemplate
    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    //查询商品信息(要求分页)
    @ResponseBody
    @RequestMapping("TbGoodsshowall")
    public Result TbGoodsshowall(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "6") Integer pageSize, String goodsName) {
        try {
            PageResult<GootsVO> result = tbGoodsService.TbGoodsshowall(pageNum, pageSize, goodsName);
            return Result.response(ResultEnums.SHOW_SUCCESS).setData(result);
        } catch (Exception e) {
            return Result.response(ResultEnums.SHOW_ERROR);
        }
    }

    //删除单个数据
    @ResponseBody
    @RequestMapping("deleteTbGoods")
    public Result deleteTbGoods(Long id) {
        try {
            tbGoodsService.deleteTbGoods(id);
            return Result.response(ResultEnums.DELETE_SUCCESS);
        } catch (Exception e) {
            return Result.response(ResultEnums.DELETE_ERROR);
        }
    }

    //删除多个数据
    @ResponseBody
    @RequestMapping("deleteallTbGoods")
    public Result deleteallTbGoods(@RequestParam(value = "ids[]") List<Long> ids) {
        try {
            tbGoodsService.deleteallTbGoods(ids);
            return Result.response(ResultEnums.DELETE_SUCCESS);
        } catch (Exception e) {
            return Result.response(ResultEnums.DELETE_ERROR);
        }
    }

    //审核通过
    @ResponseBody
    @RequestMapping("updateallTbGoodsbya")
    public Result updateallTbGoodsbya(@RequestParam(value = "ids[]") List<Long> ids) {
        try {
            tbGoodsService.updateallTbGoodsbya(ids);
            rocketMQTemplate.convertAndSend("ggw", JSONUtil.toJsonStr(ids));
            return Result.response(ResultEnums.SUCCESS);
        } catch (Exception e) {
            return Result.response(ResultEnums.ERROR);
        }
    }

    //审核驳回
    @ResponseBody
    @RequestMapping("updateallTbGoodsbyb")
    public Result updateallTbGoodsbyb(@RequestParam(value = "ids[]") List<Long> ids) {
        try {
            tbGoodsService.updateallTbGoodsbyb(ids);
            return Result.response(ResultEnums.SUCCESS);
        } catch (Exception e) {
            return Result.response(ResultEnums.ERROR);
        }
    }
}
